const ajax = require('./../api/index')// ajax文件
const pageConfig = require('./config')// 全局配置文件

var userid = sessionStorage.getItem('gq_qyh_userid')
// var userid = 'T20180123155917763328713816';
var errorCode = sessionStorage.getItem('gq_qyh_errorCode')// 错误次数

String.prototype.getParameter = function (key) {
  var re = new RegExp(key + '=([^&]*)(?:&)?')
  return this.match(re) && this.match(re)[1]
}

// 检查userid
function checkUserid (to, cb) {
  let code = location.href.getParameter('code')
  let codeStorage = sessionStorage.getItem('code')
  // console.log('执行了获取用户id', code, 'code')
  // console.log('执行了获取用户id', codeStorage, 'codeStorage')
  if (!!codeStorage && codeStorage != 'undefined') {
    cb()
  } else {
    if (code) {
      let this_path = sessionStorage.getItem('gq_qyh_path')
      if (!this_path) {
        localStorage.setItem('gq_qyh_path', to.path)// 临时存储路由
        locationhref(pageConfig.base_path, cb)
        return
      }
      sessionStorage.setItem('code', code)
      cb(this_path)
    } else {
      sessionStorage.setItem('gq_qyh_path', to.path)// 临时存储路由
      locationhref(pageConfig.base_path, cb)
    }
  }
}
function typeToAgentid (type) {
  let this_agentid = ''
  if (type.indexOf('traffic') > -1) {
    this_agentid = pageConfig.agentid.traffic
  } else
    if (type.indexOf('newsList') > -1 || type.indexOf('newsDetail') > -1) {
      this_agentid = pageConfig.agentid.news
    } else
      if (type.indexOf('noticeList') > -1 || type.indexOf('noticeDetail') > -1) {
        this_agentid = pageConfig.agentid.notic
      } else
        if (type.indexOf('roadCar') > -1) {
          this_agentid = pageConfig.agentid.roadCar
        }
  return this_agentid
}

// 获取userid
function getUserid (code, agentid, cb, path) {
  ajax.$http.post('Common/Common/getuseridHtml', { code: code, agentid: agentid },
    function (data) {
      // alert(JSON.stringify(data))
      userid = data.data.userid
      sessionStorage.setItem('gq_qyh_userid', userid)
      if (cb) {
        cb(path)
        sessionStorage.removeItem('gq_qyh_path')
        sessionStorage.removeItem('gq_qyh_errorCode')
      }
    },
    function (err) {
      if (err.msg == 'no code' || err.msg == 'code invalid') {
        sessionStorage.removeItem('gq_qyh_userid')
        // var myhrefC = location.href;
        if (errorCode) {
          errorCode = Number(errorCode)
          if (errorCode < 4) {
            errorCode++
            sessionStorage.setItem('gq_qyh_errorCode', errorCode)
            locationhref(pageConfig.base_path)
          } else {
            sessionStorage.removeItem('gq_qyh_errorCode')
            alert('内部参数错误，请联系管理员')
          }
        } else {
          errorCode = 0
          sessionStorage.setItem('gq_qyh_errorCode', errorCode)
          locationhref(pageConfig.base_path)
        }
      } else {
        alert(err.msg)
        alert('系统错误，请联系管理员')
      }
    })
}

function locationhref (url) {
  url = delQueStr(url, 'code')
  // let a = 'http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3'
  var wxurl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + pageConfig.appid + '&redirect_uri=' + encodeURIComponent(url) + '&response_type=code&scope=snsapi_base&agentid=' + 1000034 + '&state=STATE#wechat_redirect'
  // var wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + pageConfig.appid + "&redirect_uri=" + encodeURIComponent(url) + "&response_type=code&scope=snsapi_base&state=0&connect_redirect=1#wechat_redirect";
  // console.log(wxurl)
  window.location.href = wxurl
}
function delQueStr (url, ref) {
  var str = ''
  if (url.indexOf('?') != -1) {
    str = url.substr(url.indexOf('?') + 1)
  } else {
    return url
  }
  var arr = ''
  var returnurl = ''
  var setparam = ''
  if (str.indexOf('&') != -1) {
    arr = str.split('&')
    for (i in arr) {
      if (arr[i].split('=')[0] != ref) {
        returnurl = returnurl + arr[i].split('=')[0] + '=' + arr[i].split('=')[1] + '&'
      }
    }
    return url.substr(0, url.indexOf('?')) + '?' + returnurl.substr(0, returnurl.length - 1)
  } else {
    arr = str.split('=')
    if (arr[0] == ref) {
      return url.substr(0, url.indexOf('?'))
    } else {
      return url
    }
  }
}

var indexObj = {
  checkUserid: checkUserid,
  userid: function () {
    return userid
  },
  removeUserId: function () {
    sessionStorage.removeItem('gq_qyh_userid')
  }
}

// // 返回在vue模板中的调用接口
export default {
  install (Vue) {
    Vue.prototype.$user = indexObj
    Vue.user = indexObj
  },
  $user: indexObj
}

export const $user = indexObj
